home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / EGASNARF.ZIP / MOVE.C < prev    next >
C/C++ Source or Header  |  1988-01-17  |  11KB  |  579 lines

  1. #include    <math.h>
  2. #include    "snarf.h"
  3.  
  4. /****************************************************************
  5.  * SHOW things                            *
  6.  ****************************************************************/
  7.  
  8. void    show_bug(i)
  9. int    i;
  10. {
  11.     char    *p;
  12.  
  13.     if( bugs[i].dir>15 ) p = bang[bugs[i].dir-16];
  14.     else p = bug1[bugs[i].dir & 7];
  15.     newbplotxy(bugs[i].x+XORG, bugs[i].y+YORG, 16, 2, p, 3);
  16. }
  17.  
  18. void    show_man() {
  19.  
  20.     char    *p;
  21.  
  22.     p = man[ (((mans.dir&0x7f)<2) ? 4 : 0) + ((mans.key) ? 2 : 0) + (((mans.x | mans.y) & 4) ? 1 : 0) ];
  23.     newbplotxy(mans.x+XORG+3,mans.y+YORG, 16, 2, p, 3);
  24. }
  25.  
  26.  
  27. void    show_shot() {
  28.  
  29.     newbplotxy(shots.x+XORG+5,shots.y+YORG-5, 8, 1, shot[shots.dir], 3);
  30. }
  31.  
  32. void    show_score() {
  33.  
  34.     int    i;
  35.     char    temp[81];
  36.  
  37.     scorex = 296;
  38.     sprintf(scorebuf, "%8.8ld", score);
  39.     for(scoreptr=0; scorebuf[scoreptr]=='0'; scoreptr++) scorex += 14;
  40.     if( !scorebuf[scoreptr] ) {
  41.         --scoreptr;
  42.         scorex -= 14;
  43.     }
  44.     while( scorebuf[scoreptr] ) {
  45.         newbplotxy(scorex, scorey, 16, 2, digbits[scorebuf[scoreptr]-'0'], 0);
  46.         ++scoreptr;
  47.         scorex += 14;
  48.     }
  49. }
  50.  
  51. void    show_sscore() {
  52.  
  53.     int    i;
  54.     char    temp[81];
  55.  
  56.     scorex = 178;
  57.     sprintf(scorebuf, "%6.6ld", sscore);
  58.     for(scoreptr=0; scorebuf[scoreptr]=='0'; scoreptr++) scorex += 14;
  59.     if( !scorebuf[scoreptr] ) {
  60.         --scoreptr;
  61.         scorex -= 14;
  62.     }
  63.     while( scorebuf[scoreptr] ) {
  64.         newbplotxy(scorex, scorey, 16, 2, digbits[scorebuf[scoreptr]-'0'], 0);
  65.         ++scoreptr;
  66.         scorex += 14;
  67.     }
  68. }
  69.  
  70. void    clear_sscore() {
  71.  
  72.     int    i;
  73.  
  74.     sscore = 0L;
  75.     for(i=0; i<4; i++) newbplotxy(178+14*i, scorey, 16, 2, digbits[10], 0);
  76.     newbplotxy(4*14+178, scorey, 16, 2, digbits[0], 0);
  77. }
  78.  
  79.  
  80. void    show_high() {
  81.  
  82.     int    i;
  83.     char    temp[81];
  84.  
  85.     scorex = 492;
  86.     sprintf(scorebuf, "%8.8ld", highscore);
  87.     for(scoreptr=0; scorebuf[scoreptr]=='0'; scoreptr++) scorex += 14;
  88.     if( !scorebuf[scoreptr] ) {
  89.         --scoreptr;
  90.         scorex -= 14;
  91.     }
  92.     while( scorebuf[scoreptr] ) {
  93.         newbplotxy(scorex, scorey, 16, 2, digbits[scorebuf[scoreptr]-'0'], 0);
  94.         ++scoreptr;
  95.         scorex += 14;
  96.     }
  97. }
  98.  
  99.  
  100. void    clear_points() {
  101.  
  102.     int    i;
  103.  
  104.     points = 0L;
  105.     for(i=0; i<3; i++) newbplotxy(98+14*i, scorey, 16, 2, digbits[10], 0);
  106.     newbplotxy(3*14+98, scorey, 16, 2, digbits[0], 0);
  107. }
  108.  
  109.  
  110. void    show_points() {
  111.  
  112.     int    i;
  113.     char    temp[81];
  114.  
  115.     if( pointbuf[pointptr] ) {
  116.         newbplotxy(pointx, scorey, 16, 2, digbits[pointbuf[pointptr]-'0'], 0);
  117.         ++pointptr;
  118.         pointx += 14;
  119.     } else if( pointflg ) {
  120.         pointflg = 0;
  121.         pointx = 98;
  122.         sprintf(pointbuf, "%4.4ld", points);
  123.         for(pointptr=0; pointbuf[pointptr]=='0'; pointptr++) pointx += 14;
  124.         if( !pointbuf[pointptr] ) {
  125.             --pointptr;
  126.             pointx -= 14;
  127.         }
  128.     }
  129. }
  130.  
  131.  
  132. void    show_tag() {
  133.  
  134.     int    i;
  135.     char    *p;
  136.  
  137.     if( tagflg==2 ) {
  138.         --tagflg;
  139.         if( tagcount > 9 ) p = digbits[tagcount/10];
  140.         else            p = digbits[10];
  141.         newbplotxy(46, scorey, 16, 2, p, 0);
  142.     } else if( tagflg==1 ) {
  143.         --tagflg;
  144.         if( tagcount < 0 ) p = digbits[0];
  145.         else           p = digbits[tagcount%10];
  146.         newbplotxy(60, scorey, 16, 2, p, 0);
  147.     }
  148. }
  149.  
  150.  
  151. void    show_level() {
  152.  
  153.     int    i;
  154.     char    *p;
  155.  
  156.     if( curscreen > 9 ) p = digbits[curscreen/10];
  157.     else            p = digbits[10];
  158.     newbplotxy(430, scorey, 16, 2, p, 0);
  159.     p = digbits[curscreen%10];
  160.     newbplotxy(444, scorey, 16, 2, p, 0);
  161. }
  162.  
  163.  
  164. /****************************************************************
  165.  * MOVE things                            *
  166.  ****************************************************************/
  167.  
  168. void    move_shot() {
  169.     int    j;
  170.     char    *p;
  171.  
  172.     show_shot();
  173.     p = &field[41*(19-(shots.y>>4)) + (shots.x>>4) + 1];
  174.     if( !((shots.x & 15) || (shots.y & 15)) ) {
  175.         switch( shots.dir ) {
  176.          case 0:
  177.             j = *(p+1);
  178.             break;
  179.          case 1:
  180.             j = *(p-41);
  181.             break;
  182.          case 2:
  183.             j = *(p-1);
  184.             break;
  185.          case 3:
  186.             j = *(p+41);
  187.             break;
  188.          default:
  189.             j = 1;
  190.             break;
  191.         }
  192.         if( j==1 || j==-1 ) {
  193.             shots.dir = -1;
  194.             return;
  195.         }
  196.     }
  197.     switch(shots.dir) {
  198.      case 0:    /* right */
  199.         shots.x += SHOTJMP;
  200.         break;
  201.      case 1:    /* up    */
  202.         shots.y += SHOTJMP;
  203.         break;
  204.      case 2:    /* left  */
  205.         shots.x -= SHOTJMP;
  206.         break;
  207.      case 3:    /* down  */
  208.         shots.y -= SHOTJMP;
  209.         break;
  210.     }
  211.     for(j=0; j<MAXBUGS; j++) {
  212.         if( bugs[j].dir != -1 && bugs[j].dir < 4) {
  213.             if( (abs(shots.x-bugs[j].x)+abs(shots.y-bugs[j].y)) < 5 ) {
  214.                 bang_bug(j);
  215.                 shots.dir = -1;
  216.                 return;
  217.             }
  218.         }
  219.     }
  220.     show_shot();
  221. }
  222.  
  223. void    check_tag_man(i)
  224. int    i;
  225. {
  226.     if( !dead && bugs[i].dir<4 && bugs[i].dir>=0 && (abs(bugs[i].x-mans.x)+abs(bugs[i].y-mans.y))<13 ) {
  227.         setcolors(11, tagcount--);
  228.         if( tagcount<0 ) {
  229.             beep_seq(beep_dead);
  230.             dead = 15;
  231.         } else {
  232.             beep_seq(beep_tag);
  233.             tagflg=2;
  234.         }
  235.     }
  236. }
  237.  
  238.  
  239.  
  240. void    move_bug(i)
  241. int    i;
  242. {
  243.     int    j, k, shotflag;
  244.     char    *p, *r, *d, *s;
  245. /*char    temp[81];*/
  246.  
  247.     if( bugs[i].dir == -1 ) return;
  248.     show_bug(i);
  249.     switch(bugs[i].dir) {
  250.      case 0:    /* right */
  251.         bugs[i].x += BUGJMP;
  252.         break;
  253.      case 1:    /* up    */
  254.         bugs[i].y += BUGJMP;
  255.         break;
  256.      case 2:    /* left  */
  257.         bugs[i].x -= BUGJMP;
  258.         break;
  259.      case 3:    /* down  */
  260.         bugs[i].y -= BUGJMP;
  261.         break;
  262.      case 4:    /* coming alive */
  263.         p = &field[41*(19-(bugs[i].y>>4)) + (bugs[i].x>>4) + 1];
  264.         bugs[i].dir = (*p & 0x07)>>1;
  265.         show_bug(i);
  266.         return;
  267.      default:
  268.         if( --bugs[i].dir == 15 ) {
  269.             bugs[i].dir = -1;
  270.             return;
  271.         }
  272.         break;
  273.     }
  274.  
  275.     p = &field[41*(19-(bugs[i].y>>4)) + (bugs[i].x>>4) + 1];
  276.     if( bugs[i].dir < 4 && (((bugs[i].x & 15) || (bugs[i].y & 15)) == 0) ) {
  277.         if( (k=bug_dir(i))==-1 ) {
  278.             if( ++bugs[i].lost > 50 ) {
  279.                 show_bug(i);
  280.                 bang_bug(i);
  281.                 return;
  282.             }
  283.         } else bugs[i].lost = 0;
  284.         if( legal_bug_move(k, p) ) bugs[i].dir = k;
  285.         else {
  286.             while( !legal_bug_move(bugs[i].dir, p) ) {
  287.                 bugs[i].dir = rand(4);
  288.             }
  289.         }
  290.         shotflag = 0;
  291.         switch( bugs[i].dir ) {
  292.          case 0:
  293.             if( shots.y == bugs[i].y && shots.dir == 2 ) ++shotflag;
  294.             break;
  295.          case 1:
  296.             if( shots.x == bugs[i].x && shots.dir == 3 ) ++shotflag;
  297.             break;
  298.          case 2:
  299.             if( shots.y == bugs[i].y && shots.dir == 0 ) ++shotflag;
  300.             break;
  301.          case 3:
  302.             if( shots.x == bugs[i].x && shots.dir == 1 ) ++shotflag;
  303.             break;
  304.          default:
  305.             break;
  306.         }
  307.         if( shotflag ) {
  308.             j = rand(2) ? (bugs[i].dir+1)&3 : (bugs[i].dir-1)&3;
  309.             if( !legal_bug_move( j, p ) ) {
  310.                 j ^= 2;
  311.                 if( !legal_bug_move( j, p ) ) {
  312.                     j = bugs[i].dir ^ 2;
  313.                     if( !legal_bug_move( j, p ) ) j = bugs[i].dir;
  314.                 }
  315.             }
  316.             bugs[i].dir = j;
  317.         } else if( rand(10) > 7 ) {
  318.             do
  319.                 bugs[i].dir = rand(4);
  320.             while( !legal_bug_move( bugs[i].dir, p) ) ;
  321.         }
  322.         while( !legal_bug_move(bugs[i].dir, p) ) bugs[i].dir = rand(4);
  323.         check_tag_man(i);
  324.     }
  325.     show_bug(i);
  326. }
  327.  
  328.  
  329. void    move_man() {
  330.  
  331.     int    i, j;
  332.     char    *p, *r;
  333.  
  334.     if( dead ) {
  335.         if( dead==1 ) return;
  336.         mans.dir |= 0x80;
  337.         show_man();
  338.         --dead;
  339.         show_man;
  340.         return;
  341.     }
  342.     p = &field[41*(19-(mans.y>>4)) + (mans.x>>4) + 1];
  343.     if( !( (mans.dir & 0x80) || (mans.x & 15) || (mans.y & 15)) ) {
  344.         if( (i=look_man_move(mans.dir, p)) && !(i==0x70 && mans.key) ) mans.dir |= 0x80;
  345.         if( i==-1 ) {
  346.             i = tagcount;
  347.             clear_sscore();
  348.             do {
  349.                 timepass = 0;
  350.                 tagflg = 2;
  351.                 setcolors(11, tagcount);
  352.                 beepon( (50-tagcount)*300 );
  353.                 show_tag();
  354.                 show_tag();
  355.                 sscore += points;
  356.                 show_sscore();
  357.                 score += points;
  358.                 show_score();
  359.                 while( timepass < 1 ) ;
  360.             } while( tagcount-- ) ;
  361.             beepoff();
  362.             clear_points();
  363.             tagcount = i;
  364.             tagflg = 2;
  365.             show_tag();
  366.             show_tag();
  367.             show_man();
  368.             next_screen();
  369.             while( iskey() ) key();
  370.             while( !iskey() );
  371.             return;
  372.         }
  373.     }
  374.     if( !(mans.dir & 0x80) ) {
  375.         show_man();
  376.         switch(mans.dir) {
  377.          case 0:    /* right */
  378.             mans.x += MANJMP;
  379.             break;
  380.          case 1:    /* up    */
  381.             mans.y += MANJMP;
  382.             break;
  383.          case 2:    /* left  */
  384.             mans.x -= MANJMP;
  385.             break;
  386.          case 3:    /* down  */
  387.             mans.y -= MANJMP;
  388.             break;
  389.         }
  390.         show_man();
  391.     }
  392.     if( ((mans.x & 15) || (mans.y & 15)) == 0 ) {
  393.         p = &field[ 41*(19-(mans.y>>4)) + (mans.x>>4) + 1];
  394.         if( (*p & 0xf0) == 0x40 ) {
  395.             beep_seq(beep_goodies);
  396.             disp_field( (mans.x>>4)+1, 19-(mans.y>>4) );
  397.             points += values[*p & 0x0f];
  398.             ++pointflg;
  399.             *p = 0;
  400.         } else if( (*p & 0xf0) == 0x80 ) {
  401.             while(tagcount<50) {
  402.                 ++tagcount;
  403.                 setcolors(11, tagcount);
  404.                 beepon( (50-tagcount)*300 );
  405.                 tagflg = 2;
  406.                 show_tag();
  407.                 show_tag();
  408.                 timepass = 0;
  409.                 while(timepass < 1) ;
  410.             }
  411.             beepoff();
  412.             disp_field( (mans.x>>4)+1, 19-(mans.y>>4) );
  413.             *p = 0;
  414.         } else if( !mans.key && *p == 0x60 ) {
  415.             beep_seq(beep_goodies);
  416.             disp_field( (mans.x>>4)+1, 19-(mans.y>>4) );
  417.             show_man();
  418.             ++mans.key;
  419.             show_man();
  420.             *p = 0;
  421.         } else if( mans.key && *p == 0x70 ) {
  422.             beep_seq(beep_lock);
  423.             disp_field( (mans.x>>4)+1, 19-(mans.y>>4) );
  424.             show_man();
  425.             mans.key = 0;
  426.             show_man();
  427.             *p = 0;
  428.         } else if( (*p & 0xfe) == 0x14 ) {
  429.             r = p-41;
  430.             while( r > field ) {
  431.                 if( (*r & 0xfe) == 0x14 ) {
  432.                     show_man();
  433.                     mans.y = (19-(r-field)/41)*16;
  434.                     show_man();
  435.                     break;
  436.                 }
  437.                 r -= 41;
  438.             }
  439.             if( r <= field ) {
  440.                 r = p+41;
  441.                 while( r < (field+820) ) {
  442.                     if( (*r & 0xfe) == 0x14 ) {
  443.                         show_man();
  444.                         mans.y = (19-(r-field)/41)*16;
  445.                         show_man();
  446.                         break;
  447.                     }
  448.                     r += 41;
  449.                 }
  450.             }
  451.             beep_seq(beep_teleport);
  452.         }
  453.         if( newmandir != -1 ) {
  454.             show_man();
  455.             mans.dir = newmandir;
  456.             newmandir = -1;
  457.             show_man();
  458.         }
  459.         
  460.     } else if( newmandir != -1 && newmandir==(mans.dir^2)) {
  461.         show_man();
  462.         mans.dir = newmandir;
  463.         newmandir = -1;
  464.         show_man();
  465.     }
  466. /*
  467.     for(i=0; i<MAXBUGS; i++) {
  468.         check_tag_man(i);
  469.     }
  470. */
  471. }
  472.  
  473. void    bang_bug(i)
  474. int    i;
  475. {
  476.     show_bug(i);
  477.     bugs[i].dir = 20;
  478.     show_bug(i);
  479.     beep_seq(beep_kill);
  480. }
  481.  
  482.  
  483. void    make_bug() {
  484.  
  485.     int    r, i, j, k, diff;
  486.  
  487.     if(dead) return;
  488.     for(i=0; i<MAXBUGS; i++) {
  489.         if( bugs[i].dir == -1 ) {
  490.             diff = 1000;
  491.             for(j=0; j<MAXPITS; j++) {
  492.                 if( pitlist[j].x == -1 ) break;
  493.  
  494. r = man_dist(field, (pitlist[j].x>>4)+1, 19-(pitlist[j].y>>4), (mans.x>>4)+1, 19-(mans.y>>4) );
  495.  
  496.                 if( r < diff ) {
  497.                     k = j;
  498.                     diff = r;
  499.                 }
  500.             }
  501.             if( (j>1) && (rand(10)>7) ) {
  502.                 while( k==(r=rand(j)) ) ;
  503.                 k = r;
  504.             }
  505.             bugs[i].x = pitlist[k].x;
  506.             bugs[i].y = pitlist[k].y;
  507.             bugs[i].dir = 15;
  508.             bugs[i].kind = 1;
  509.             bugs[i].lost = 0;
  510.             show_bug(i);
  511.             return;
  512.         }
  513.     }
  514. }
  515.  
  516.  
  517. int    legal_bug_move( i, p )
  518. int    i;
  519. char    *p;
  520. {
  521.     char    c, d;
  522.  
  523.     switch( i ) {
  524.      case 0:
  525.         c = *(p+1);
  526.         break;
  527.      case 1:
  528.         c = *(p-41);
  529.         break;
  530.      case 2:
  531.         c = *(p-1);
  532.         break;
  533.      case 3:
  534.         c = *(p+41);
  535.         break;
  536.      default:
  537.         return 0;
  538.         break;
  539.     }
  540.     d = c&0xf0;
  541.     if( c==1 || d==0x10 || d==0x20 || d==0x70 ) return 0;
  542.     return 1;
  543. }
  544.  
  545.  
  546. int    look_man_move( i, p )
  547. int    i;
  548. char    *p;
  549. {
  550.     char    c, d;
  551.  
  552.     switch( i ) {
  553.      case 0:
  554.         c = *(p+1);
  555.         break;
  556.      case 1:
  557.         c = *(p-41);
  558.         break;
  559.      case 2:
  560.         c = *(p-1);
  561.         break;
  562.      case 3:
  563.         c = *(p+41);
  564.         break;
  565.      default:
  566.         return 0;
  567.         break;
  568.     }
  569.     if( c == -1 ) {
  570.         if( i == 2 ) return 1;
  571.         else return -1;
  572.     }
  573.     d = c & 0xf0;
  574.     if( d == 0xf0 || d== 0x20 || c == 1 || (d==0x10 && (c&0x0e)!=0x04) ) return (int)c;
  575.     if( c == 0x70 && !mans.key ) return (int)c;
  576.     return 0;
  577. }
  578.  
  579.